﻿using System;

using System.Collections.Generic;
using System.Text;
using Acid.Math;

namespace Acid.Interaction
{
    public class DeviceUtil
    {
        private HTCGSensor sensor;
        private Vector3    oldpoint;
        private Vector3    newpoint;
        private Vector3    current;
        private float      lerpOffset;
        private float      lerp;

        public DeviceUtil(HTCGSensor sensor, int interval)
        {
            this.sensor     = sensor;
            this.lerp       = 0.0f;
            this.lerpOffset = (float)(1.0f / (float)interval);
            this.oldpoint   = new Vector3();
            this.newpoint   = new Vector3();
            this.current    = new Vector3();
        }
        
        
        /// <summary>
        /// Updates the Device 
        /// </summary>
        public void Update()
        {
            this.lerp += (float)this.lerpOffset;

            if (this.lerp > 1.0f)
            {
                this.oldpoint = this.newpoint;

                this.newpoint = this.sensor.GetGVector();

                this.newpoint.Normalize();

                this.lerp = 0.0f;
            }

            this.current = Vector3.Lerp(oldpoint, newpoint, this.lerp);
        }

        /// <summary>
        /// Current Device Vector
        /// </summary>
        public Vector3 Current
        {
            get
            {
                return this.current;
            }
        }

        public void    ConvertToDeviceOrientation(Vector2 v, out Vector2 orientatedVector)
        {
            Vector2 up       = new Vector2(this.current.X, this.current.Y);
            Vector2 left     = new Vector2(this.current.Y, this.current.X);
            up               = Vector2.Normalize(up);
            left             = Vector2.Normalize(left);
            orientatedVector = new Vector2(Vector2.Dot(left, v), Vector2.Dot(up, v));
        }

  
        public Vector3 DeviceNormal
        {
            get
            {
                return new Vector3();
            }
        }

    }
}
